package engine.newUi
{
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.GameDefine;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import flashx.textLayout.formats.TextAlign;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;
	
	public class FastTrackUI extends Sprite
	{
		public static var _self:FastTrackUI;
		
		private var _model:ModelLocator;
		
		private var _bind:BindManage;
		
		private var _bindLevIndex:int	=	-1;
		private var _bindPetIndex:int	=	-1;
		public var text0:TextField;
		public var text1:TextField;
		public var text2:TextField;
		public var text3:TextField;
		public var text4:TextField;
		public var text5:TextField;
		
		/**文本容器*/
		private var textArr:Array;
		
		private var btn0:NxButton;
		private var btn1:NxButton;
		private var btn2:NxButton;
		private var btn3:NxButton;
		private var btn4:NxButton;
		private var btn5:NxButton;
		private var btnArr:Array;
		
		/**血统队列中是否显示增加队列超链接*/
		private var isShowArr :Array;
		
		private var statusArr:Array;
		
		private var equipArr:Array;
		
		private var allinfoArr:Array;
		
		/**血统升级队列个数 */		
		private var statusNum:int	=	0;
		
		/**装备升级队列个数 */		
		private var equipNum:int	=	0;
		
		private var statusBtn:NxButton;
		
		private var equipBtn:NxButton;
		
		/**是否显示消CD提示框*/
		private var _isShow:Boolean	=	true;	
		
		private var _statusUpIndex:int			=	0;
		private var _equipUpIndex:int			=	1;
		private var _worldTraveIndex:int		=	2;
		
		private static var _isShowStatus:Boolean;
		
		/**文本信息容器*/
		private var _textArrInfo:Array;
		
		/**需要显示的链接个数*/
		private var _maxLinkNum:int	=	8;
		//时间
		private var _date:Date ; 
		//倒计时
		private var _time:Timer ;
		//喂养次数
		private var _PetCDCount:int  ; 
		//时间字符串
		private var _petCdStr:String = "" ; 
		public function FastTrackUI()
		{
			super();
			Init();
		}
		
		public static function getInstance():FastTrackUI
		{
			if(_self == null)
			{
				_self	=	new FastTrackUI();
			}
			return _self
		}
		
		private function Init():void
		{		
			_date		=	new Date();
			isShowArr	=	[];
			textArr		=	[];
			btnArr		=	[];
			_textArrInfo	=	[];
			_model		=	ModelLocator.getInstance();
			_bind 		=	BindManage.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_REQUEST_FEEDTIMES_INC_TIME),getTimesCD)	
			//statusBtn	=	ResManager.
			//InitBtn();_bindPetIndex
			
			InitText();
			//在CD模块注册
			PlayCDTimerLogic.getInstance().resgisAll( 2,statusUp );
			PlayCDTimerLogic.getInstance().resgisAll( 3,equipUp );
			PlayCDTimerLogic.getInstance().resgis(5,emulaUp);
			PlayCDTimerLogic.getInstance().resgisAll( 10,worldTravel );
			PlayCDTimerLogic.getInstance().resgisAll(12,pkListUp);
			PlayCDTimerLogic.getInstance().resgisAll(21,pulseCD);
			_bindPetIndex	=	_bind.BindProp(_model.scene.Role,"FeedTimes",this,"FeedTimesFun");
			_bindLevIndex	=	_bind.BindProp(_model.scene.Role,"Level",this,"LevelChageFun");
			
			
			_time=new Timer(1000);
			_time.addEventListener(TimerEvent.TIMER,playTime);
			
		}
		public function set LevelChageFun(val:int):void
		{
			addIndustryUp();
			PlayCDTimerLogic.getInstance().upData();
			if(_PetCDCount>0)
			{
				petShow();
			}
			
		}
		/**
		 * 绑定宠物次数 
		 */		
		public function set FeedTimesFun(val:int):void
		{
			_PetCDCount = val ; 
//			if(val==0)
//			{
				petShow();
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_FEEDTIMES_INC_TIME);
//			}
		}
		/**
		 * CD模块回调函数
		 * 血统强化 2
		 * @param val CD时间
		 * @param arr 队列信息
		 * 
		 */		
		private function statusUp(val:int,arr:Array):void
		{	
			//if(isShowStatus == false)retur
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","peerageprop"));
			if(_model.scene.roleLevel < needLevel )return;
			var obj:Object	=	new Object();
			//var addStr:String	=	StaticIni.getSwfLangStr("UIString_1000092");
			var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_2");
			if(StatusShopUI.getInstance().checkGuide == 1)
			{			
				obj["info"]			=	[arr,str,1];
				_textArrInfo[arr[0]]	=	obj;
			}
			else
			{
				_textArrInfo[arr[0]]	=	-1;
			}
			showText();
		}
		
		/**
		 * 游历CD回调函数 
		 * @param val
		 * @param col
		 */		
		private function worldTravel(val:int ,arr:Array):void
		{
			
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","walk"));
			if(_model.scene.roleLevel < needLevel )return;
			//如果游离次数完了。不显示
			var obj:Object	=	new Object();
			//游历江湖的链接属性,0 -- 不显示,1 -- 显示
			if(int(_model.scene.Role.GetProperties("WorldTravelDisplayer")) == 1
				&& GameData.SceneType	!=	GameDefine.SceneType_CS)
			{		
				var str:String		=	StaticIni.getSwfLangStr("UIString_1000284");
				if(val > 0)
				{
					obj["info"]			=	[arr,String(val),0];
				}
				else
				{
					obj["info"]			=	[arr,str,1];
				}
				_textArrInfo[arr[0]]	=	obj;
			}
			else
			{
				//当次数用完后 赋值-1来表示
				_textArrInfo[arr[0]]	=	-1;
			}
			showText();
		}
		/**
		 * CD模块回调函数
		 * 装备升级 3
		 * @param val CD时间
		 * @param arr 队列信息
		 */		
		private function equipUp(val:int,arr:Array):void
		{
			var obj:Object	=	new Object();
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","equip"));
			if(_model.scene.roleLevel < needLevel )return;
			//var addStr:String	=	StaticIni.getSwfLangStr("UIString_1000093");
			var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_3");
			if(CheckModifiedEnableLogic.getInstance().checkGuide == 1)
			{
				obj["info"]			=	[arr,str,1];
				_textArrInfo[arr[0]]	=	obj;
			}
			else
			{
				_textArrInfo[arr[0]]	=	-1;
			}	
			showText();			
		}
		
		/**
		 * 拜师 
		 * @param val
		 * @param arr
		 * 
		 */		
		private function emulaUp(val:int,col:int):void
		{
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","emulate"));
			if(_model.scene.roleLevel < needLevel )return;
			var arr:Array		=	new Array(5,col,val,0);	
			var obj:Object		=	new Object();
//			if(int(_model.scene.Role.GetProperties("HerbNum")) > 0) 
//			{
				var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_6");
				if(val > 0)
				{
					obj["info"]			=	[arr,String(val),0];
				}
				else
				{
					obj["info"]			=	[arr,str,1];
				}
				_textArrInfo[arr[0]]	=	obj;
//			}
//			else
//			{
//				_textArrInfo[arr[0]]	=	-1;
//			}		
			showText();
		}
		
		/**
		 * 排名竞技场 
		 * @param val
		 * @param arr
		 * 
		 */		
		private function pkListUp(val:int,arr:Array):void
		{
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","battle"));
			if(_model.scene.roleLevel < needLevel )return;
			//如果挑战次数完了不显示
			var obj:Object		=	new Object();
			var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_5");
			if(int(_model.scene.Role.GetProperties("CurCloneChallengeTimes")) > 0
				&& GameData.SceneType	!=	GameDefine.SceneType_CS ) 
			{
				if(val > 0)
				{
					obj["info"]			=	[arr,String(val),0];
				}
				else
				{
					obj["info"]			=	[arr,str,1];
				}
				_textArrInfo[arr[0]]	=	obj;
			}
			else
			{
				//当次数用完后 赋值-1来表示
				_textArrInfo[arr[0]]	=	-1;
			}	
			showText();
		}
		
		/**
		 *经脉CD 
		 * @param val
		 * @param arr
		 * 
		 */
		private function pulseCD(val:int,arr:Array):void
		{
//			if(!(PulseLogic.getInstance().checkIsCanStudy()))return;
			if(_model.scene.Role.getPlayerLevel()<16)return;
			var obj : Object = new Object();
			var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_9");
			if(val > 0)
			{
				obj["info"]			=	[arr,String(val),0];
			}
			else
			{
				obj["info"]			=	[arr,str,1];
			}
			_textArrInfo[arr[0]]	=	obj;
			showText();
		}
		
		/**
		 * 
		 */
		private function getTimesCD(e:CustomMsgEvent):void
		{
//			if(_PetCDCount!=0||e.pList[0]==0||e.pList.length==0)return ;  
//			_date.time =e.pList[0]*1000;
//			_time.start();
		}
		/**
		 * 宠物界面超链接
		 */
		private function petShow(val:String=null):void
		{
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","pet"));
			if(_model.scene.roleLevel < needLevel )return;
			var obj:Object		=	new Object();
			var  arr:Array		=	new Array(15,0,0,0);	
			if(_PetCDCount>0) 
			{		
				var str:String			=	StaticIni.getSwfLangStr("MainUIStr_middle_8");
				if(_petCdStr!= "")
				{
//					arr		=	new Array(15,0,0,0);	
					obj["info"]			=	[arr,String(_petCdStr),3];
				}
				else
				{
//					 arr		=	new Array(15,0,0,0);	
					obj["info"]			=	[arr,str,1];
				}
				_textArrInfo[arr[0]]	=	obj;
			}
			else
			{
//				//当次数用完后 赋值-1来表示
				_textArrInfo[arr[0]]	=	-1;
			}
			showText();
		}
		/**
		 * 宠物时间倒计时
		 */
		private function playTime(e:TimerEvent):void
		{
			if(_date.time>=1000)
			{
				_date.time-=1000;
				_petCdStr=MysterySellLogic.getInstance().getTimeStr(_date);
				petShow();
			}
			else
			{
				_petCdStr ="";
				petShow();
				_time.reset();
			}
		}
		/**
		 * 江湖产业
		 * @param val
		 * @param arr
		 * 
		 */		
		private function addIndustryUp():void
		{
			var needLevel:int	=	int(StaticIni.getIniVar("fastentrance","industry"));
			if(_model.scene.roleLevel < needLevel )return;
			//产业不在CD列表中，所以自定义模拟一个数组
			var arr:Array		=	[0,0,0,1];		
			var obj:Object		=	new Object();
			//var addStr:String	=	StaticIni.getSwfLangStr("UIString_1000093");
			var str:String		=	StaticIni.getSwfLangStr("MainUIStr_middle_7");
			(textArr[arr[1]+statusNum] as TextField).visible	=	true;
			obj["info"]			=	[arr,str,1];
			_textArrInfo[arr[0]]	=	obj;
			showText();
			(textArr[statusNum+arr[1]] as TextField).visible	=	true;
		}
		
		/**
		 * 链接显示控制 
		 * 
		 */		
		private function showText():void
		{
			var temp:int	=	0;
			var arr:Array;
			var itemBtnt:NxButton;	
			var text:TextField;
			for(var j:int=0;j < _maxLinkNum;j++)
			{
				(textArr[j] as TextField).htmlText	=	"";
				(btnArr[j] as NxButton).visible	=	false
			}
			for(var i:int=0;i < _textArrInfo.length;i++)
			{
				if( _textArrInfo[i] == null || _textArrInfo[i] == -1 )continue
				text	=	textArr[temp] as TextField;
				text.visible	=	true;
				arr	=	_textArrInfo[i]["info"];
				if(arr[2] == 0)
				{
					itemBtnt	=	btnArr[temp] as NxButton;		
					itemBtnt.visible	=	true;
					itemBtnt.data.HandleProperties("info",arr[0]);
					itemBtnt.data.HandleProperties("Cdtime",arr[1]);
				}
				text.htmlText	=	getLinkStr(arr[0],arr[1],arr[2]);
				if( itemBtnt != null)
				{
					itemBtnt.x	=	text.x+text.width+10;
				}
				temp++;
			}
		}
		
		/**
		 * 初始化文本 
		 * 
		 */		
		private  function InitText():void
		{
			var txtStyle:StyleSheet	=	new StyleSheet();
			txtStyle.setStyle("a:link"	,{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","link"),textDecoration:"underline"});
			//txtStyle.setStyle("a:visited",{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","link")});
			txtStyle.setStyle("a:hover"	,{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","hover"),textDecoration:"none"});	
			
			for(var i:int = 0;i < _maxLinkNum;i++)
			{
				var itemTxt:TextField				=	new TextField();
				itemTxt.x				=	35;
				itemTxt.y				=	100+i*20;
				itemTxt.autoSize		=	TextAlign.LEFT;
				itemTxt.selectable		=	false;
				itemTxt.styleSheet		=	txtStyle;
				itemTxt.visible			=	false;
				itemTxt.htmlText		=	"";
				this.addChild(itemTxt);
				textArr.push(itemTxt);	
				itemTxt.addEventListener(TextEvent.LINK,onTextClickHandle); 
				itemTxt.filters = [ NxLabel.glow ];
				var itemBtn:NxButton		=	new NxButton();
				itemBtn.skin				=	"Button_52";
				itemBtn.width				=	15;
				itemBtn.height				=	15;
				itemBtn.label				=	"";
				itemBtn.x					=	150;
				itemBtn.y					=	100+i*20;
				itemBtn.visible				=	false;
				
				itemTxt.filters = [ NxLabel.glow ];
				btnArr.push(itemBtn);
				this.addChild(itemBtn);
				itemBtn.addEventListener(MouseEvent.CLICK,onClearBtnClickHandle);
				itemBtn.addEventListener(MouseEvent.ROLL_OUT,clearCDOutTips);
				itemBtn.addEventListener(MouseEvent.ROLL_OVER,clearCDOverTips);
			}
			
		}
		
		/**
		 * 清cd消耗tips 
		 * @param e
		 * 
		 */		
		private function clearCDOverTips(e:MouseEvent):void
		{
			var val:int		=	(e.target.data as BaseObject).GetProperties("Cdtime");
			var info:Array	=	(e.target.data as BaseObject).GetProperties("info");
			var	num:int	=	val/60;	
			var needMoney:String;
			if(info[0] == 12 || info[0] == 10 || info[0] == 21)
			{
				//排名竞技场消耗 经脉
				needMoney	=	getNeedMagicCoin(val);
			}
			else
			{
				
				needMoney	=	String(Math.floor(num * 0.5)+1);
			}
//			TipsManage.showTips(
//				StaticIni.getSwfLangStrVar("UIString_1000095",[needMoney]));
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStrVar("UIString_1000095",[needMoney]));
		}
		
		
		/**
		 * 返回所需要的龙金币
		 * @return
		 */
		private function getNeedMagicCoin(val:int):String
		{
			var minutes :Number;
			var seconds:Number;
			var needStr:String	=	"";
			minutes		=	Math.floor(val / 60);
			seconds		=	val  - minutes * 60;
			if (seconds > 0) 
			{
				minutes++;
			}
			needStr	=	String(1 * minutes);
			return needStr;
		}
		
		private function clearCDOutTips(e:MouseEvent):void
		{
//			TipsManage.hideTips();;		
			TipsMainLogic.instance.hide();
		}
		
		/**
		 *	初始化按钮 
		 * 
		 */		
		private function InitBtn():void
		{
			statusBtn		=	new NxButton();
			statusBtn.skin	=	"";
			statusBtn.x		=	100;
			statusBtn.y		=	100;
			this.addChild(statusBtn);
		}
		
		/**
		 * 消除CD按钮 
		 * @param e
		 * 
		 */		
		private function onClearBtnClickHandle(e:MouseEvent):void
		{
			var arr:Array	=	(e.target.data as BaseObject).GetProperties("info");
			var val:int	=	(e.target.data as BaseObject).GetProperties("Cdtime");
//			SystemSender.getInstance().CustomMsgEx(
//				CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,[int(arr[0]),int(arr[1])]);
			onClearCDHandle(arr,val);
		}
		/**
		 * 给超链接赋值 
		 * @param arr ,str,val(区分超链接是0 CD时间中，1 空闲，2曾队列)
		 * @return 
		 */		
		private function getLinkStr(arr:Array,text:String,val:int):String
		{
			var str:String;
			var timeStr:String;
			var timeArr:Array	=	[];
			if(val == 0)
			{
				timeStr	=	text;
				text	=	"  "+PlayCDTimerLogic.getInstance().getTimerStr( int(text),true );
				//timeArr = 	text.split(":");
				if(int(arr[0])==2)
				{
					text	=	StaticIni.getSwfLangStrVar("MainUIStr_left_2",[text]);
				}
				else if(arr[0]==5)
				{
					text	=	StaticIni.getSwfLangStrVar("UIString_1000340",[text]);
				}
				else if(arr[0]==10)
				{
					text	=	StaticIni.getSwfLangStrVar("UIString_1000285",[text]);
				}
				else if(arr[0]==12)
				{
					text	=	StaticIni.getSwfLangStrVar("UIString_1000341",[text]);
				}
				else if(arr[0]==21)
				{
					text = StaticIni.getSwfLangStrVar("UIString_1000710",[text]);
				}
			}else if(val==3)
			{
				
			}
			else
			{
				timeStr	=	arr[2];
			}
			str	=	"<a href=\"event:" 
						+  arr[0] + "," +arr[1] + "," + timeStr +","+ arr[3] +","+ val +"\">"
						+ text + 
					"</a>"	;
			return str;		
		}
		
		/**
		 * 超链接点击事件
		 * @param e
		 * 
		 */		
		private function onTextClickHandle(e:TextEvent):void
		{
			var linkStr:String = 	e.text;
			var arr:Array	   = 	linkStr.split(",");	
			//var val:int	=	(e.target.data as BaseObject).GetProperties("Cdtime");
			switch(int(arr[4]))
			{
				//消除CD消息
				case 0:
//						SystemSender.getInstance().CustomMsgEx(
//							CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,[int(arr[0]),int(arr[1])]);
					onClearCDHandle(arr,arr[2]);
					break
				//CD空闲，打开超链接
				case 1:
					openUI(arr);
					break
				//开新队列消息
				case 2:
					SystemSender.getInstance().CustomMsgEx(
						CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_CD_QUEUE,[int(arr[0])]);
					break
			}		
		}
		
		/**
		 * 清除CD
		 * @param e
		 * 
		 */		
		private function onClearCDHandle(arr:Array,val:int):void
		{
			var	yesBtnT		:String			=	StaticIni.getSwfLangStr("SellUI_Sure");
			var	noBtn		:String			=	StaticIni.getSwfLangStr("AlertEx_1");
			var num:int						=	val/60;
			var needMoney:String;	
			if(arr[0] == 12 || arr[0] == 10 || arr[0] == 21 )
			{
				//排名竞技场消耗  经脉
				needMoney	=	getNeedMagicCoin(val);
			}
			else
			{
				
				needMoney	=	String(Math.floor(num * 0.5)+1);
			}
			var AlertUIShowString : String;
			if(arr[0] == 21)//经脉
			{
				AlertUIShowString = "UIString_1000713";
			}
			else
			{
				AlertUIShowString = "UIString_1000306";
			}
			AlertUI.showAlertEx("",StaticIni.getSwfLangStrVar(AlertUIShowString,[needMoney]),
				[yesBtnT,noBtn],AlertUI.YES|AlertUI.NO,clickClearCD);
			function clickClearCD( type:int, str:String, clickData:* ):void
			{
				if(AlertUI.YES == type )
				{
					SystemSender.getInstance().CustomMsgEx(
						CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD,[int(arr[0]),int(arr[1])]);	
				}
			}
			
		}
		/**
		 * 超链接点击打开对应界面 
		 */		
		private function openUI(arr:Array):void
		{	
			var str:String;
			switch(int(arr[0]))
			{
				case 0:
					//阵法
					str	=	"75041044";
					break
				case 2:
					//阵法
					str	=	"75041002";
					break
				case 3:
					//强化
					str	=	"75041001";
					break
				case 5:
					//拜师
					str	=	"75041026";
					break
				case 10:
					//游历
					str	=	"75041043";
					break
				case 12:
					//排名竞技场
					str	=	"75011012";
					break
				case 15:
					//宠物界面
					PetLoic.getInstance().nxTableSelect = 1 ; 
					str	=	"75041056";
					break ;
				case 21:
					str = "75041035";
					break;
				
			}
			
			SystemSender.getInstance().CustomMsg(
				CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, str);
		}
			
	}
}